using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace G2007.CodeGen.Library.History
{


    public class Process
    {


        public static void SetState ( Library.Generator.Compiler newCompiler ,
            Library.Generator.Compiler oldCompiler )
        {
            // true for isNew and then added to the
            // file if deleted, make sure the deleted flag is set
            // and that the transforms don't create it


            foreach ( Library.Data.Class newClass in newCompiler.Classes )
            {
                bool foundClass = false;
                foreach ( Library.Data.Class oldClass in oldCompiler.Classes )
                {
                    if ( oldClass.Name.Equals ( newClass.Name ) )
                    {
                        foundClass = true;

                        foreach ( Library.Data.ClassAttributes caNew in newClass.ClassAttribs )
                        {
                            bool foundAttrib = false;


                            foreach ( Library.Data.ClassAttributes caOld in oldClass.ClassAttribs )
                            {
                                if ( caOld.Name.Equals ( caNew.Name )
                                    && !caOld.Type.Equals ( caNew.Type ) )
                                {
                                    // This entry has had its property type changed
                                    // but not its actual name, so it needs to be
                                    // set to removed and set to new so that it can
                                    // be dropped and added to the table
                                    // might need to do this in a separate SQL statement
                                    
                                    caNew.IsRemoved = true;
                                    caNew.IsNew = true;
                                    foundAttrib = true;
                                }


                                if ( caOld.Name.Equals ( caNew.Name )
                                    && caOld.PropertyName.Equals ( caNew.PropertyName ) )
                                {
                                    foundAttrib = true;
                                }

                            }

                            if ( !foundAttrib )
                            {
                                caNew.IsNew = true;
                            }

                        }

                        List<Library.Data.ClassAttributes> removedAttributes = new List<G2007.CodeGen.Library.Data.ClassAttributes> ();

                        foreach ( Library.Data.ClassAttributes caOld in oldClass.ClassAttribs )
                        {
                            bool foundAttrib = false;

                            foreach ( Library.Data.ClassAttributes caNew in newClass.ClassAttribs )
                            {
                                if ( caNew.Name.Equals ( caOld.Name ) )
                                {
                                    foundAttrib = true;
                                }
                            }


                            if ( !foundAttrib )
                            {
                                // this has been removed from the new object, add it to the 
                                removedAttributes.Add ( caOld );
                            }
                           
                        }

                        newClass.RemovedAttributes = removedAttributes.ToArray ();

                    }
                }

                if ( !foundClass )
                {
                    newClass.IsNew = true;
                }
            }


			List<Library.Data.Class> removedClasses = new List<G2007.CodeGen.Library.Data.Class> ();

            foreach ( Library.Data.Class oldClass in oldCompiler.Classes )
            {
                // now we need to find all the entries
                // that have been removed from from the old one
                // when compared to the new one. if we do this we need
                // to fill the removed objects list for this object
                // with that entry, then we can handle removing it from 
                // the transforms without effecting any of the current XSLT
                // that have been written

                bool foundOldClass = false;

                foreach ( Library.Data.Class newClass in newCompiler.Classes )
                {
                    if ( newClass.Name.Equals ( oldClass.Name ) )
                    {
                        foundOldClass = true;
                        break;
                    }
                }

                if ( !foundOldClass )
                {
                    // add this class to the removed class attributes
                    // list of the class (which we have yet to create )
					removedClasses.Add ( oldClass );
                }

            }

			newCompiler.RemovedClasses = removedClasses.ToArray ();


			

        }



        public static bool SaveHistory ( string[] data , string appPath , string filename )
        {
            string fileLocation = 
                System.Configuration.ConfigurationSettings.AppSettings[ "HISTORY_FOLDER" ] + filename;



            if ( File.Exists ( fileLocation ) )
            {
                File.Delete ( fileLocation );
            }

            using ( StreamWriter sw = File.CreateText ( fileLocation ) )
            {
                foreach ( string d in data )
                {
                    sw.WriteLine ( d );
                }

                sw.Close ();
            }

            return true;

        }


        // this will search for files with the same name in the history
        // file and return only the one which has the most latest date
        public static bool GetHistory ( string filename ,
            string appPath ,
            out Library.Generator.Compiler compiler, bool auditLog )
        {

            string fileLocation = 
                System.Configuration.ConfigurationSettings.AppSettings[ "HISTORY_FOLDER" ] + filename;

            if ( File.Exists ( fileLocation ) )
            {
                compiler = new G2007.CodeGen.Library.Generator.Compiler (
                    fileLocation , appPath );

				string error;

				compiler.Compile ( out error , false , false , auditLog );

                return error.Length==0;
            }
            else
            {

                compiler = null;
                return false;
            }

        }



    }
}
